﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using de.rwth.rz.campus.sputils.Security;

namespace de.rwth.rz.campus.sharepointutils.UsersAndGroups
{
    /// <remarks>
    /// 
    /// Copyright 2010 RWTH Aachen, CAMPUS Labs
    /// 
    /// Licensed under the Apache License, Version 2.0 (the "License");
    /// you may not use this file except in compliance with the License.
    /// You may obtain a copy of the License at
    ///
    /// http://www.apache.org/licenses/LICENSE-2.0
    ///
    /// Unless required by applicable law or agreed to in writing, software
    /// distributed under the License is distributed on an "AS IS" BASIS,
    /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    /// See the License for the specific language governing permissions and
    /// limitations under the License.
    /// 
    /// This class can be used to send emails to SharePoint groups.
    /// For every SharePoint user in a SharePoint group an email is generated.
    /// Be aware of the load of your SMTP server.
    /// 
    /// </remarks>
    public class Groups
    {
        /// <summary>
        /// Gets all groups from a WSS web site
        /// </summary>
        /// <param name="spWebSite">The Website, where the groups should be detected</param>
        /// <returns>The list of found groups</returns>
        public static List<SPGroup> GetAllGroups(SPWeb spWebSite)
        {
            List<SPGroup> spGroups = new List<SPGroup>();
            if (spWebSite != null)
            {
                foreach (SPGroup spGroup in spWebSite.Groups)
                {
                    spGroups.Add(spGroup);
                }
            }
            return spGroups;
        }

        /// <summary>
        /// Tries to find a group by its name, searchs the Site and the SiteCollection.
        /// The Site has priority.
        /// </summary>
        /// <param name="spWebSite">The Website, where the group should be searched</param>
        /// <param name="groupName">The name of the group</param>
        /// <returns>NULL, if no group is found, else the SPGroup object</returns>
        public static SPGroup FindGroup(SPWeb spWebSite, String groupName)
        {
            SPGroup returnGroup = null;
            if (spWebSite != null)
            {
                using (SPSite siteCollection = SPSiteSecurity.GetElevatedSite(spWebSite.Site))
                {
                    using (SPWeb elevatedWebSite = siteCollection.OpenWeb(spWebSite.ID))
                    {
                        foreach (SPGroup spGroup in elevatedWebSite.Groups)
                        {
                            if (spGroup.Name == groupName)
                            {
                                returnGroup = spGroup;
                                return returnGroup;
                            }
                        }
                        foreach (SPGroup spGroup in elevatedWebSite.SiteGroups)
                        {
                            if (spGroup.Name == groupName)
                            {
                                returnGroup = spGroup;
                                return returnGroup;
                            }
                        }
                    }
                }
            }
            return returnGroup;
        }

        /// <summary>
        /// Gets all users of a SharePoint group
        /// </summary>
        /// <param name="spGroup">The SharePoint group</param>
        /// <returns>A list of the user belonging to the group</returns>
        public static List<SPUser> GetUsersBy(SPGroup spGroup)
        {
            List<SPUser> spUsers = new List<SPUser>();
            if (spGroup != null)
            {
                foreach (SPUser spUser in spGroup.Users)
                {
                    spUsers.Add(spUser);
                }
            }
            return spUsers;
        }

        /// <summary>
        /// Gets all users of a SharePoint group corresponding to a website, !runs with elevated rights!
        /// </summary>
        /// <param name="spGroup">The SharePoint group</param>
        /// <param name="webSite">The corresponding website</param>
        /// <returns>A list of the user belonging to the group</returns>
        public static List<SPUser> GetUsersBy(SPGroup spGroup, SPWeb webSite)
        {
            List<SPUser> spUsers = new List<SPUser>();
            if (spGroup != null)
            {
                using (SPSite siteCollection = SPSiteSecurity.GetElevatedSite(webSite.Site))
                {
                    using (SPWeb elevatedWebSite = siteCollection.OpenWeb(webSite.ID))
                    {
                        SPGroup elevatedGroup = elevatedWebSite.SiteGroups.GetByID(spGroup.ID);
                        foreach (SPUser spUser in elevatedGroup.Users)
                        {
                            //filter system account
                            if (spUser.UserToken != elevatedWebSite.CurrentUser.UserToken)
                                spUsers.Add(spUser);
                        }
                    }
                }
            }
            return spUsers;
        }
    }
}
